﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kocic.RepositoryManagement.Crm4;
//using SampleXrm.Xrm;
using Sample.Run.Crm4;
using Kocic.RepositoryManagement;

namespace Sample.Run
{
    public static class CRM4Samples
    {
        public static void Run()
        {
            //#region Compound Example

            ///* The CRM repository implementation works the same as the other repository manager implementations.
            // * The following example shows how to create a contact, and tie it to an existing account. */

            //using (new XrmExecutionContext<SampleXrmDataContext>("CRM"))
            //{
            //    //Cleanup
            //    var existingAccount = XrmRepositoryManager<account, SampleXrmDataContext>.Retrieve(a => a.name == "Derp Incorporated").SingleOrDefault();

            //    if(existingAccount != null)
            //    {
            //        XrmRepositoryManager<account, SampleXrmDataContext>.DeleteCrmEntityObject(existingAccount);
            //    }

            //    //A XrmContextDeferrer instance deffers the SaveChanges operation and essentially pushes all changes to CRM when it's disposed.
            //    using (new XrmContextDeferrer<SampleXrmDataContext>())
            //    {
            //        //Managers instantiated in this scope will first clear the cache for the entity they manage
            //        using (new XrmCacheManager())
            //        {
            //            using (ContactRepositoryManager contactManager = new ContactRepositoryManager())
            //            {
            //                contact newContact = new contact();
            //                newContact.firstname = "Derp";
            //                newContact.lastname = "Derpington";

            //                //Save new contact to CRM
            //                contactManager.Creating += (s, e) =>
            //                {
            //                    Console.WriteLine(e.DataObject.firstname + " " + e.DataObject.lastname + " has been created.");
            //                };

            //                contactManager.Save(newContact, SaveMethod.Create);

            //                account newAccount = new account();
            //                newAccount.name = "Derp Incorporated";

            //                //Save account entity
            //                XrmRepositoryManager<account, SampleXrmDataContext>.SaveCrmEntityObject(newAccount, SaveMethod.Create, () =>
            //                {
            //                    //Create relationship between contact and account.
            //                    XrmRelationshipManager<SampleXrmDataContext>.CreateRelationship(new XrmRelationshipInfo()
            //                    {
            //                        Name = "account_contacts",
            //                        Primary = newAccount,
            //                        Related = newContact
            //                    });

            //                    Console.WriteLine(newAccount.name + " created and relationship set.");
            //                });
            //            }
            //        }
            //    }
            //}
            //#endregion
        }
    }
}
